var QQMapWX = require('../../resource/utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp();
Page({
  data: {
    jx_goods:"",
    page:1,
    limit:10,
    getPosution:'',
    tabbar:{},
    showView: true,
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    cardCur: 0,
    tower: [],
    moRenShop:"",
    nomorelist: true,
  },

  // 用于实现点击“核算”时，来显示与隐藏整个“conts”，这一部分其实是利用了面板的显示与隐藏功能  
  change: function () {
    let that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  close: function () {
    let that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },

  onLoad(){
    var that = this;
    const value = wx.getStorageSync('openid');
    app.changeTabBar();
    wx.setNavigationBarTitle({
      title: '蜂语网络'
    }),
    this.towerSwiper('tower');
    qqmapsdk = new QQMapWX({ key: '4B7BZ-LD46U-C7FVV-4ZFNG-YVZT3-7MFGJ' });
    wx.getLocation({
      type: 'wgs84',
      success(res){
        const latitude = res.latitude
        const longitude = res.longitude
        that.qqmap(latitude + ',' + longitude);
      }
    })
    //轮播
    app.util.request({
      url: 'admin/WxGoodsApi/rotation',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if(res.data.code==0){
          var datalun = res.data.data
          for (var i = 0; i < datalun.length; i++) {
            datalun[i]['rotation_image'] = app.globalData.url + datalun[i]['rotation_image'];
          }
          that.setData({
            tower: datalun
          })
        }
        
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
   
    // 默认门店
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        app.util.request({
          url: 'admin/WxGoodsApi/nearby_store',
          data: {
            uid:value,
            longitude: latitude,
            latitude: longitude
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res){
            if(res.data.code==0){
              var data = res.data.data;
              for (var c = 0; c < data.length; c++) {
                if (data[c].default_shop === 1) {
                  that.setData({
                    moRenShop: data[c].shop_name
                  })
                }
              }
              if (that.data.moRenShop == "") {
                that.setData({
                  moRenShop: data[0].shop_name
                })
              }
            }
            
          },
          fail: function (res) {
            wx.showToast({
              title: '服务器响应失败',
              icon: 'none',
              duration: 2000
            })
          }
        });
      }
    });
    
// 精选
    app.util.request({
      url: 'admin/WxGoodsApi/jx_goods',
      data: {
        page: this.data.page,
        limit: this.data.limit,
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        var data = res.data.data
        if(res.data.code==0){
          
          if (!data) {
            that.setData({
              nomorelist: true
            })
          } else {
            for (var i = 0; i < data.length; i++) {
              data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
            }
            that.setData({
              jx_goods: data
            })
          }
        }
       
        
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  onShow:function(){
    const value = wx.getStorageSync('openid');
    app.util.request({
      url: 'admin/WxGoodsApi/shopcarList',
      method: 'post',
      data: {
        uid: value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data.data;
        var num = 0;
        if (data === undefined || data.length == 0) {
          app.globalData.tabbar.list[2]['dott'] = num;
          app.setData();
        }else{
          for (var i = 0; i < data.length; i++) {
            num += data[i]['goods_num'];
          }
          app.globalData.tabbar.list[2]['dott'] = num;
          app.setData();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  qqmap: function (location){
    var _this = this;
    qqmapsdk.reverseGeocoder({

      location: location || '', 
      success: function (res){
        // console.log(res.result.address);
        _this.setData({
          getPosution: app.globalData.getPosution= res.result.address
        })
      },
      fail: function (error) {
        console.error(error);
      }
    })
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      towerList: list
    })
  },

  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  tabbar(e){
    app.tabbar(e);
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },

  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.towerList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        towerList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        towerList: list
      })
    }
  },
  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function (){// 精选++
    var page = this.data.page;
    var pages = ++page;
    var that = this;
    that.setData({
      page: pages
    })
    app.util.request({
      url: 'admin/WxGoodsApi/jx_goods',
      data: {
        page: pages,
        limit: this.data.limit,
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data.data;
        if (data === undefined || data.length == 0){
          that.setData({
            nomorelist: false
          })
        }else{
          for (var i = 0; i < data.length; i++){
            data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
          }
          var jx_goods = that.data.jx_goods;
          for (var i = 0; i < data.length; i++){
            jx_goods.push(data[i]);
          }
          that.setData({
            jx_goods: jx_goods,
          })
        }
      },
      fail:function(res){
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
    }


});